package kr.saeildamil.androidstudy_second31;

import java.util.ArrayList;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;

public class GestureDump extends Activity {
	ArrayList<String>arGesture=new ArrayList<String>();
	TextView mResult;
	GestureDetector mDetector;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gesturedump);
		
		mResult=(TextView) findViewById(R.id.ch31_gesturedump_result);
		mDetector=new GestureDetector(this , mGestureListener);
		mDetector.setOnDoubleTapListener(mDoubleTapListener);
	}
	
	OnDoubleTapListener mDoubleTapListener=new OnDoubleTapListener() {
		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			appendText("onSingleTapConfirmed");
			return false;
		}
		@Override
		public boolean onDoubleTapEvent(MotionEvent e) {
			appendText("onDoubleTapEvent");
			return false;
		}
		@Override
		public boolean onDoubleTap(MotionEvent e) {
			appendText("onDoubleTap");
			return false;
		}
	};
	
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	};
	
	OnGestureListener mGestureListener=new OnGestureListener() {
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			appendText("onSingleTapUp");
			return false;
		}
		@Override
		public void onShowPress(MotionEvent e) {
			appendText("onShowPress");
			
		}
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			String text=String.format("Scroll:(%d, %d) -->(%d,%d)(%d,%d)",
					(int)e1.getX(),(int)e1.getY(),(int)e2.getX(),(int)e2.getY(),distanceX,distanceY);
			appendText("onScroll");
			appendText(text);
			return false;
		}
		@Override
		public void onLongPress(MotionEvent e) {
			appendText("onLongPress");
		}
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			String text=String.format("Filing:(%d, %d) -->(%d,%d) Velocity(%d,%d)",
					(int)e1.getX(),(int)e1.getY(),(int)e2.getX(),(int)e2.getY(),velocityX,velocityY);
			appendText("onFling");
			appendText(text);
			return false;
		}
		@Override
		public boolean onDown(MotionEvent e) {
			String text=String.format("Down:(%d,%d)",(int)e.getX(), (int)e.getY());
			appendText("onDown");
			appendText(text);
			return false;
		}
	};
	
	private void appendText(String text){
		if(arGesture.size()>15){
			arGesture.remove(0);
		}
		arGesture.add(text);
		StringBuilder sb=new StringBuilder();
		for(String str:arGesture){
			sb.append(str+"\n");
		}
		mResult.setText(sb.toString());
	}
}
